package com.ruoyi.worker.service.impl;

import cn.hutool.core.util.IdcardUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.worker.constant.Constant;
import com.ruoyi.worker.domain.*;
import com.ruoyi.worker.mapper.*;
import com.ruoyi.worker.service.AttendanceGroupService;
import com.ruoyi.worker.service.CompanyService;
import com.ruoyi.worker.service.EmploymentPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName: AttendanceGroupServiceImpl
 * @Description:
 * @Author: 刘松山
 * @Date: 2021/9/8 17:31
 */
@Service
public class AttendanceGroupServiceImpl implements AttendanceGroupService {

    @Autowired
    private AttendanceGroupMapper attendanceGroupMapper;

    @Autowired
    private ExcelMapper excelMapper;

    @Autowired
    private TimeShiftMapper timeShiftMapper;

    @Autowired
    private EmployeeSalaryMapper employeeSalaryMapper;

    @Autowired
    private ISysPostService postService;

    @Autowired
    private EmploymentPlanService employmentPlanService;

    @Autowired
    private CompanyService companyService;
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private TermWorkerMapper termWorkerMapper;



    @Override
    public int deleteAttendanceGroupIds(Long[] ids) {
        long id = ids[0].longValue();
        AttendanceGroup a = attendanceGroupMapper.getAttendanceGroup(id);

        attendanceGroupMapper.deleteAttendanceGroupIds(ids);
        AttendanceGroup resultAttendanceGroup = this.countAttendanceGroup(a);
        if(null ==resultAttendanceGroup){
            //明細全部刪除了 刪除汇总记录
            this.delALlAttendancestatistics(a);
        }
        return 1;
    }

    @Override
    public int addAttendanceGroup(AttendanceGroup a) {
        return attendanceGroupMapper.addAttendanceGroup(a);
    }

    @Override
    public int updateAttendanceGroup(AttendanceGroup a) {
        if (null != a.getCompanyName() && !"".equals(a.getCompanyName())) {
            a.setStatusFlag(1);
        }
        attendanceGroupMapper.updateAttendanceGroup(a);
        //如果数据都修改正常 修改汇总记录状态为 待确认
        List<AttendanceGroup> list = this.listAttendanceGroup(a);
        int  flag =0;
        for(AttendanceGroup attendanceGroup :list){
           if( attendanceGroup.getStatusFlag()==0){
               flag =1;
               break;
           }
        }
        if(flag ==0){
            AttendanceInfo attendanceInfo = new AttendanceInfo();
            attendanceInfo.setStatus(0);
            attendanceInfo.setWorkTime(a.getWorkTime());
            attendanceGroupMapper.modifyAttendanceInfo(attendanceInfo);
        }

        return 1;
    }

    @Override
    public AttendanceGroup getAttendanceGroup(long id) {
        AttendanceGroup a = attendanceGroupMapper.getAttendanceGroup(id);
        return a;
    }

    @Override
    public List<AttendanceInfo> listAttendanceInfo(AttendanceInfo a) {
        List<AttendanceInfo> list = attendanceGroupMapper.listAttendanceInfo(a);
//        for (AttendanceInfo attendanceInfo : list) {
//            attendanceInfo.setTotalNum(attendanceInfo.getAbnormalNum() + attendanceInfo.getNormalNum()+attendanceInfo.getErrorDataNum());
//        }
        return list;
    }

    @Override
    public AttendanceGroup countAttendanceGroup(AttendanceGroup a) {
        return attendanceGroupMapper.countAttendanceGroup(a);
    }

    @Override
    public int delALlAttendanceGroup(AttendanceGroup a) {
        return attendanceGroupMapper.delALlAttendanceGroup(a);
    }


    @Override
    public int delALlAttendanceSum(AttendanceGroup a) {
        return attendanceGroupMapper.delALlAttendanceSum(a);
    }

    @Override
    public int modifyAttendanceInfo(AttendanceInfo attendanceInfo) {
        List<AttendanceInfo> attendanceInfos = attendanceGroupMapper.listAttendanceInfo(attendanceInfo);
        if (attendanceInfos.size() > 0) {
            //生成薪资明细
            translateAttendanceToSalary(attendanceInfos);
        } else {
            return 0;
        }
        AttendanceGroup a = new AttendanceGroup();
        a.setWorkTime(attendanceInfo.getWorkTime());
        //0 待确认 1 待处理  2 已经驳回 3  已确认 4 待审核
        a.setCheckFlag(3);
        attendanceGroupMapper.modifyAttendanceSum(a);
        return attendanceGroupMapper.modifyAttendanceInfo(attendanceInfo);
    }

    @Override
    public int addAttendanceStatistics(AttendanceInfo a) {
        return attendanceGroupMapper.addAttendanceStatistics(a);
    }

    @Override
    public int changAttendanceStatus(long id, int attendanceFlag) {
        return attendanceGroupMapper.changAttendanceStatus(id, attendanceFlag);
    }

    @Override
    public int delALlAttendancestatistics(AttendanceGroup a) {
        return attendanceGroupMapper.delALlAttendancestatistics(a);
    }

    @Override
    public int modifyAttendanceStatistics(AttendanceInfo a) {
        return attendanceGroupMapper.modifyAttendanceStatistics(a);
    }

    @Override
    public List<AttendanceGroup> listAttendanceGroup(AttendanceGroup attendanceGroup) {
        return attendanceGroupMapper.listAttendanceGroup(attendanceGroup);
    }

    @Override
    public void translateAttendanceToSalary(List<AttendanceInfo> attendanceInfos) {
        AttendanceInfo info = attendanceInfos.get(0);
        List<TemporaryWorkerDomain> temporaryWorkers = attendanceGroupMapper.getTemporaryWorkerAttendanceSum(info.getWorkTime(), info.getOrderCode());

        List<DailySalary> dailySalaries = new ArrayList<DailySalary>();
        Double salaryTotal = 0.00;
        DailySalaryTotal dailySalaryTotal = new DailySalaryTotal();
        dailySalaryTotal.setWorkerNeedNo(info.getOrderCode());
        dailySalaryTotal.setWorkingDate(info.getWorkTime());
        dailySalaryTotal.setDemandQuantity(info.getTotalNum());

        for (TemporaryWorkerDomain excelToolDomain1 : temporaryWorkers) {
            DailySalary dailySalary = new DailySalary();
            dailySalary.setWorkerName(excelToolDomain1.getWorkerName());
            dailySalary.setIdCard(excelToolDomain1.getIdCard());
            dailySalary.setSex(excelToolDomain1.getSex());
            dailySalary.setTimeShift(excelToolDomain1.getTimeShift());
            dailySalary.setPhoneCode(excelToolDomain1.getPhoneCode());
            if (excelToolDomain1.getGroupName() != null) {
                dailySalary.setGroup(excelToolDomain1.getGroupName());
            }
            if (excelToolDomain1.getCompanyName() != null) {
                dailySalary.setCompany(excelToolDomain1.getCompanyName());
            }


            dailySalary.setAttendanceStatus(excelToolDomain1.getAttendanceFlag());
            if (excelToolDomain1.getAttendanceFlag() != 2) {
                dailySalary.setSalary(0.00);
            } else {
                //渠道商价格启用
                if (Constant.PRICE_STATUS_ENABLE.equals(excelToolDomain1.getPriceStatus())) {
                    if (Constant.DAY_SHIFT.equals(excelToolDomain1.getTimeShiftName())) {
                        dailySalary.setSalary(excelToolDomain1.getDayPrice().doubleValue());
                    } else if (Constant.NIGHT_SHIFT.equals(excelToolDomain1.getTimeShiftName())) {
                        dailySalary.setSalary(excelToolDomain1.getNightPrice().doubleValue());
                    }
                    salaryTotal += dailySalary.getSalary();
                }


            }
            dailySalaries.add(dailySalary);
        }
        dailySalaryTotal.setSalaryTotal(salaryTotal);

        employeeSalaryMapper.insertDailySalaryTotal(dailySalaryTotal);

        for (DailySalary dailySalary2 : dailySalaries) {
            dailySalary2.setSalaryDailyTotalId(dailySalaryTotal.getId());
        }

        employeeSalaryMapper.insertDailySalary(dailySalaries);
    }

    @Override
    public int modifyAttendanceSum(AttendanceGroup attendanceGroup) {
        return attendanceGroupMapper.modifyAttendanceSum(attendanceGroup);
    }

    @Override
    public int countAttendanceByIdCard(AttendanceGroup attendanceGroup) {
        return attendanceGroupMapper.countAttendanceByIdCard(attendanceGroup);
    }

    @Override
    public void saveWorkerGroup(List<ExcelToolDomain> excelToolDomain) {
        attendanceGroupMapper.saveWorkerGroup(excelToolDomain);
    }

    @Override
    public List<ExcelToolDomain> getAttendanceCompany(ExcelToolDomain excelToolDomains) {
        return attendanceGroupMapper.getAttendanceCompany(excelToolDomains);
    }

    @Override
    public void saveAttendanceSum(List<ExcelToolDomain> excelToolDomain) {
        attendanceGroupMapper.saveAttendanceSum(excelToolDomain);
    }

    @Override
    public List<ExcelToolDomain> getAttendanceInfos(ExcelToolDomain excelToolDomain) {
        return attendanceGroupMapper.getAttendanceInfos(excelToolDomain);
    }

    @Override
    public List<ExcelToolDomain> getAttendanceSum(ExcelToolDomain excelToolDomain) {
        List<ExcelToolDomain> list = attendanceGroupMapper.getAttendanceSum(excelToolDomain);

        return list;

    }

    @Override
    public AttendanceInfo getAttendanceStatistics(AttendanceInfo a) {
        return attendanceGroupMapper.getAttendanceStatistics(a);
    }

    @Override
    public int countAttendanceSum(AttendanceInfo a) {
        return attendanceGroupMapper.countAttendanceSum(a);
    }

    @Override
    public int countAttendanceSumByIdCard(AttendanceGroup attendanceGroup) {
        return attendanceGroupMapper.countAttendanceSumByIdCard(attendanceGroup);
    }

    @Override
    public List<AttendanceStatisticsImg> getAttendanceStatisticsImg(AttendanceStatisticsImg a) {
        return attendanceGroupMapper.getAttendanceStatisticsImg(a);
    }

    @Override
    public int addAttendanceStatisticsImg(List<AttendanceStatisticsImg> list) {
        return attendanceGroupMapper.addAttendanceStatisticsImg(list);
    }

    @Override
    public int delAttendanceStatisticsImg(AttendanceStatisticsImg attendanceStatisticsImg) {
        AttendanceStatisticsImg a = attendanceGroupMapper.getImg(attendanceStatisticsImg);
        int flag = attendanceGroupMapper.delAttendanceStatisticsImg(attendanceStatisticsImg);
        if (flag > 0) {
            //删除服务器文件
            String filePath = a.getUrl();
            filePath = filePath.substring(filePath.indexOf("images") - 1, filePath.length());
            FileUtils.deleteFile(filePath);
        }
        return flag;
    }

    @Override
    public AjaxResult importExcel(List<ExcelToolDomain> ExcelToolDomains) {
        ExcelToolDomain e = ExcelToolDomains.get(0);

        String workNeedCode = e.getOrderCode();
        AttendanceInfo info = new AttendanceInfo();
        info.setWorkTime(ExcelToolDomains.get(0).getWorkTime());
        info.setOrderCode(workNeedCode);
        AttendanceInfo resultInfo = this.getAttendanceStatistics(info);
        //0 待确认 1 待处理  2 已经驳回 3  已确认 4 待审核
        if (resultInfo != null && resultInfo.getStatus() == 3) {
            return AjaxResult.error("需求单号为：" + workNeedCode + "已经确认，不能再导入了");
        }

        OperationNeedPojo op = new OperationNeedPojo();
        op.setNeedTime(DateUtils.parseDateToStr("yyyy-MM-dd", e.getWorkTime()));
        op.setTaskNumber(workNeedCode);
        OperationNeedPojo opBack = employmentPlanService.getworkNeedInfo(op);
        if (opBack == null || opBack.getSumNumber() == 0) {
            return AjaxResult.error("用工需求单和用工日期对应的记录不存在,请核对");
        }

        //基础校验
        String message = this.baseCheck(ExcelToolDomains);
        if (!"".equals(message)) {
            return AjaxResult.error(message);
        }

        //查询是否导入过了
        // 0 未导入过 直接保存 >0
        // 导入过 获取表中数据和当前数据对比 有变化的修改 多出的新增 其他不变
        AttendanceGroup a = new AttendanceGroup();
        a.setWorkTime(ExcelToolDomains.get(0).getWorkTime());
        a.setOrderCode(workNeedCode);
        int importNum = this.getImportNum(a);

        //字段值是否存在表中的校验
        message = this.checkCorrect(ExcelToolDomains, importNum);
        if (!"".equals(message)) {
            return AjaxResult.error(message);
        }

        //业务校验 黑名单 在备注字段打标记
        this.setBlack(ExcelToolDomains);
        // 不允许临时工连续上班 判断 在备注字段打标记
        this.setToilwornWorker(ExcelToolDomains, DateUtils.getDifferDay(e.getWorkTime(), -1));
        //校验是否有长期工 临时工不允许是在职长期工
        this.setTernWorker(ExcelToolDomains);

        //获取渠道商考勤信息
        List<ExcelToolDomain> list = this.getAttendanceCompany(ExcelToolDomains.get(0));
        //补充需求单号
        for (ExcelToolDomain excelToolDomain : list) {
            excelToolDomain.setOrderCode(e.getOrderCode());
        }

        List<ExcelToolDomain> addList = new ArrayList<>();
        if (importNum == 0) {
            //第一次导入 数据不全 默认是数据错误 考勤状态默认未知
            for (ExcelToolDomain excelToolDomain : ExcelToolDomains) {
                excelToolDomain.setAttendanceFlag(0);
                excelToolDomain.setStatusFlag(0);
            }

            this.recordComparison(ExcelToolDomains, list);
            this.saveAttendanceSum(ExcelToolDomains);

            //汇总
            this.delALlAttendancestatistics(a);
            this.saveAttendanceStatistics(e,
                    workNeedCode, opBack.getSumNumber(), 1);
        } else {
            // 先和数据库中已经存在的对比
            String workTime = DateUtils.getFormatDate(e.getWorkTime(), "yyyyy-MM-dd");
            List<ExcelToolDomain> recordList = attendanceGroupMapper.listAttendanceGroupByWorkTime(workTime);

            ExcelToolDomains = this.recordComparison(ExcelToolDomains, recordList);

            //和来源小程序的对比

            ExcelToolDomains = this.recordComparison(ExcelToolDomains, list);
            this.delALlAttendanceSum(a);
            this.saveAttendanceSum(ExcelToolDomains);

        }
        //汇总
        this.delALlAttendancestatistics(a);
        int status = 0;
        for (ExcelToolDomain excelToolDomain : ExcelToolDomains) {
            if (excelToolDomain.getStatusFlag() == 0) {
                // 数据有缺失的 时候 为待处理状态
                status = 1;
                break;
            }
        }
        this.saveAttendanceStatistics(e, workNeedCode, opBack.getSumNumber(), status);

        return AjaxResult.success();
    }

    /**
     * @description: 获取需求单号workNeedCode导入几次了
     * @author 刘松山
     * @date 2021/12/21 10:18
     */
    public int getImportNum(AttendanceGroup a) {
        AttendanceGroup resultAttendanceGroup = this.countAttendanceGroup(a);
        int importNum = 0;
        if (resultAttendanceGroup != null) {
            importNum++;


        }
        return importNum;
    }

    //校验excel表格中填写的数据是否正确
    public String checkCorrect(List<ExcelToolDomain> ExcelToolDomains, int importNum) {

        String message = "";
        TimeShift timeShift = new TimeShift();
        timeShift.setStatus(1);
        List<TimeShift> timeShiftList = timeShiftMapper.getTimeShiftList(timeShift);
        Company company = new Company();
        company.setStatus(1);
        List<Company> companyList = companyService.getCompanyList(company);

        int k = 0;
        for (int i = 0; i < ExcelToolDomains.size(); i++) {

            ExcelToolDomain ed = ExcelToolDomains.get(i);

            ed.setIdCard(ed.getIdCard());
            ed.setImportNum(importNum + 1);
            int flagA = 0;
            int flagB = 0;
            int flagC = 0;
            if(ed.getTimeShiftName() != null){
                ed.setTimeShiftName(ed.getTimeShiftName().trim());
                for (TimeShift t : timeShiftList) {
                    if ( ed.getTimeShiftName().equals(t.getName())) {
                        ExcelToolDomains.get(i).setTimeShift(t.getId());
                        flagA = 1;
                        break;
                    }
                }
            }


            if (null != ed.getCompanyName()) {
                ed.setCompanyName(ed.getCompanyName().trim());
                for (Company c : companyList) {

                    if (ed.getCompanyName().equals(c.getCompany())) {
                        ExcelToolDomains.get(i).setCompanyId(c.getId());
                        flagC = 1;
                        break;
                    }
                }
            } else {
                flagC = 1;
            }


            if (flagA == 0) {
                message += "第" + (k + 3) + "行," + ed.getWorkerName() + "班次数据不正确,请核对";

            }

            if (flagC == 0) {

                message += "第" + (k + 3) + "行," + ed.getWorkerName() + "渠道商数据不正确,请核对";

            }
            k++;
        }
        return message;
    }


    /**
     * @description: 保存临时工汇总信息
     * @author 刘松山
     * @date 2021/12/21 15:17
     */
    public void saveAttendanceStatistics(ExcelToolDomain e, String workNeedCode, int totalNum, int status) {
        AttendanceInfo attendanceInfo = new AttendanceInfo();
        attendanceInfo.setWorkTime(e.getWorkTime());
        attendanceInfo.setTotalNum(totalNum);
        attendanceInfo.setOrderCode(workNeedCode);
        attendanceInfo.setStatus(status);
        attendanceInfo.setTimeShiftName(e.getTimeShiftName());
        this.addAttendanceStatistics(attendanceInfo);
    }

    /**
     * @description: 导入临时工考勤信息 基础校验 包括 身份证合法性 年龄 重复数据 已经不为空
     * @author 刘松山
     * @date 2021/12/21 13:12
     */
    public String baseCheck(List<ExcelToolDomain> ExcelToolDomains) {
        String message = "";

        Map<String, Object> valueMap = redisCache.getCacheMap("ageRange");
        int startAge = Integer.parseInt(valueMap.get("startAge") + "");
        int endAge = Integer.parseInt(valueMap.get("endAge") + "");

        ExcelToolDomain e = ExcelToolDomains.get(0);
        for (int i = 0; i < ExcelToolDomains.size(); i++) {
            if (null ==ExcelToolDomains.get(i).getOrderCode() || "".equals(ExcelToolDomains.get(i).getOrderCode())) {
                message += "第" + (i + 3) + "需求单号为空,请核对";
            }
            if (!e.getOrderCode().equals(ExcelToolDomains.get(i).getOrderCode())) {
                message += "第" + (i + 3) + "行需求单号不一致,请核对";
            }
            if (null == ExcelToolDomains.get(i).getWorkerName() || "".equals(ExcelToolDomains.get(i).getWorkerName())) {
                message += "第" + (i + 3) + "行员工姓名为空,请核对";

            }else{
                ExcelToolDomains.get(i).setWorkerName(ExcelToolDomains.get(i).getWorkerName().trim());
            }

//            if (null == ExcelToolDomains.get(i).getGroupName()) {
//                message += "第" + (i + 3) + "行," + ExcelToolDomains.get(i).getWorkerName() + "岗位为空,请核对";
//
//            }
            if (null == ExcelToolDomains.get(i).getIdCard()) {
                message += "第" + "行," + ExcelToolDomains.get(i).getWorkerName() + "身份证为空,请核对";

            }else{
                ExcelToolDomains.get(i).setIdCard(ExcelToolDomains.get(i).getIdCard().trim().toUpperCase());
                if (DateUtils.extractYearMonthDayOfIdCard(ExcelToolDomains.get(i).getIdCard()) == null) {
                    message += "第" + (i + 3) + "行," + ExcelToolDomains.get(i).getWorkerName() + "身份证格式不对,请核对";

                } else {
                    //校验年龄
                    int age = IdcardUtil.getAgeByIdCard(ExcelToolDomains.get(i).getIdCard());
                    if (age < startAge) {
                        message += "第" + (i + 3) + "行," + ExcelToolDomains.get(i).getWorkerName() + "年龄小于" + startAge + "岁，请核对";
                    }
                    if (age > endAge) {
                        message += "第" + (i + 3) + "行," + ExcelToolDomains.get(i).getWorkerName() + "年龄大于" + endAge + "岁，请核对";
                    }
                }
            }

        }
        List<ExcelToolDomain> lst = ExcelToolDomains.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
                        () -> new TreeSet<>(Comparator.comparing(o -> o.getWorkerName() + "_" + o.getIdCard() + "_" + o.getTimeShiftName()))),
                ArrayList::new));

        if (lst.size() < ExcelToolDomains.size()) {
            message += "信息重复了" + (ExcelToolDomains.size() - lst.size()) + "条,请核对";

        }
        return message;
    }

    /**
     * @description: 设置黑名单
     * @author 刘松山
     * @date 2021/12/21 13:48
     */
    public void setBlack(List<ExcelToolDomain> ExcelToolDomains) {
        List<RzBlockWorker> list = attendanceGroupMapper.listBlackList();
        for (RzBlockWorker w : list) {
            for (ExcelToolDomain e : ExcelToolDomains) {
                if (w.getIdCard().equals(e.getIdCard())) {

                    if (null == e.getRemark()) {
                        e.setRemark("黑名单 ");
                    } else {
                        e.setRemark(e.getRemark() + ";黑名单 ");
                    }

                    break;
                }
            }
        }

    }

    /**
     * @description: 设置疲劳上班 规则就是昨天上班的 今天不能要
     * @author 刘松山
     * @date 2021/12/21 14:46
     */
    public void setToilwornWorker(List<ExcelToolDomain> ExcelToolDomains, String workTime) {
        List<ExcelToolDomain> list = attendanceGroupMapper.listAttendanceGroupByWorkTime(workTime);
        for (ExcelToolDomain a : list) {
            for (ExcelToolDomain e : ExcelToolDomains) {
                if (a.getIdCard().equals(e.getIdCard())) {
                    if (null == e.getRemark()) {
                        e.setRemark("与上一排班重复 ");
                    } else {
                        e.setRemark(e.getRemark() + ";与上一排班重复 ");
                    }

                    break;
                }
            }
        }
    }

    /**
     * @description: 对比导入的excel 数据和数据库中的记录 关键值不同 保存 相同修改
     * 对比关键字 姓名、性别、班次、渠道、班组
     * @author 刘松山
     * @date 2021/12/21 15:28
     */
    public List<ExcelToolDomain> recordComparison(List<ExcelToolDomain> ExcelToolDomains, List<ExcelToolDomain> list) {

        for (ExcelToolDomain e : list) {
            if (e.getCompanyName() == null || e.getGroupName() == null) {
                e.setStatusFlag(0);
                e.setAttendanceFlag(0);

            } else {
                e.setStatusFlag(1);
                e.setAttendanceFlag(2);
            }
        }

        for (ExcelToolDomain a : ExcelToolDomains) {
            String idCardA = a.getIdCard();
            String timeShiftNameA = a.getTimeShiftName();
            String workNameA = a.getWorkerName();
//            int sexA = 0;
//            if(a.getSex() !=null){
//                sexA = a.getSex();
//            }

            String resultA = idCardA.toUpperCase() + "_" + timeShiftNameA + "_" + workNameA ;

            int flag = 0;
            for (ExcelToolDomain e : list) {
                String idCardB = e.getIdCard();
                String timeShiftNameB = e.getTimeShiftName();
                String workNameB = e.getWorkerName();
//                int sexB = 0;
//                if(e.getSex() !=null){
//                    sexB = e.getSex();
//                }
                String resultB = idCardB.toUpperCase() + "_" + timeShiftNameB + "_" + workNameB ;
                if (resultB.equals(resultA)) {

                    if (a.getCompanyName() == null) {
                        if (e.getCompanyName() != null) {
                            a.setCompanyName(e.getCompanyName());
                            a.setCompanyId(e.getCompanyId());
                        }
                    } else {
                        if (e.getCompanyName() == null) {
                            e.setCompanyName(a.getCompanyName());
                            e.setCompanyId(a.getCompanyId());
                        }
                    }

                    if (a.getGroupName() == null) {
                        if (e.getGroupName() != null) {
                            a.setGroupName(e.getGroupName());
                            a.setGroupCode(e.getGroupCode());
                        }

                    } else {
                        if (e.getGroupName() == null) {
                            e.setGroupName(a.getGroupName());
                            e.setGroupCode(a.getGroupCode());
                        }
                    }

                    if (a.getCompanyName() == null || a.getGroupName() == null) {
                        a.setStatusFlag(0);
                        a.setAttendanceFlag(0);

                    } else {
                        a.setStatusFlag(1);
                        a.setAttendanceFlag(2);
                    }
                    if (e.getCompanyName() == null || e.getGroupName() == null) {
                        e.setStatusFlag(0);
                        e.setAttendanceFlag(0);

                    } else {
                        e.setStatusFlag(1);
                        e.setAttendanceFlag(2);
                    }


                    flag = 1;
                    break;
                }
            }
            if (flag == 0) {
                if (a.getCompanyName() == null || a.getGroupName() == null) {
                    a.setStatusFlag(0);
                    a.setAttendanceFlag(0);

                } else {
                    a.setStatusFlag(1);
                    a.setAttendanceFlag(2);
                }


            }
        }


        //合并
        ExcelToolDomains.addAll(list);
        //去除重复的 姓名+性别+身份证+班次+渠道商+班组
        List<ExcelToolDomain> lst = ExcelToolDomains.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
                        () -> new TreeSet<>(Comparator.comparing(o -> o.getWorkerName()

                                + "_" + o.getIdCard().toUpperCase()
                                + "_" + o.getTimeShiftName()
                                + "_" + o.getCompanyName()
                                + "_" + o.getGroupName()
                        ))),
                ArrayList::new));
        List<ExcelToolDomain> resultList = this.setSame(lst);
        return resultList;


    }

    public List<ExcelToolDomain> setSame(List<ExcelToolDomain> list) {
        for (int i = 0; i < list.size(); i++) {
            ExcelToolDomain a = list.get(i);
            String idCardA = a.getIdCard();
            String timeShiftNameA = a.getTimeShiftName();
            String workNameA = a.getWorkerName();

//            int sexA = 0;
//            if(a.getSex() !=null){
//                sexA = a.getSex();
//            }
            String resultA = idCardA.toUpperCase() + "_" + timeShiftNameA + "_" + workNameA ;
            for (int j = i + 1; j < list.size(); j++) {
                ExcelToolDomain e = list.get(j);

                String idCardB = e.getIdCard();
                String timeShiftNameB = e.getTimeShiftName();
                String workNameB = e.getWorkerName();

//                int sexB = 0;
//                if(e.getSex() !=null){
//                    sexB = e.getSex();
//                }
                String resultB = idCardB.toUpperCase() + "_" + timeShiftNameB + "_" + workNameB ;
                //有重复值 那么渠道商或者岗位信息不同 数据有差异
                if (resultA.equals(resultB)) {
                    a.setStatusFlag(0);
                    a.setAttendanceFlag(0);
                    e.setStatusFlag(0);
                    e.setAttendanceFlag(0);
                }
            }
        }
        return list;
    }

    // 校验是否是在职长期工
    public void setTernWorker(List<ExcelToolDomain> list) {
        TermWorker t = new TermWorker();
        List<TermWorkerExcel> ternWorkerlist = termWorkerMapper.selectTermWorkerTask(t);
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < ternWorkerlist.size(); j++) {
                if (list.get(i).getIdCard().equals(ternWorkerlist.get(j).getIdCard())) {
                    if (null != list.get(i).getRemark()) {
                        list.get(i).setRemark(list.get(i).getRemark() + ";长期工名单重复");
                    } else {
                        list.get(i).setRemark("长期工名单重复");
                    }

                    break;
                }
            }
        }
    }


}
